package operation.modules.workReport.controller;

import operation.modules.workReport.entity.WorkPlaceEntity;
import operation.modules.workReport.service.WorkPlaceService;
import operation.util.ReqResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* 地点 控制层
*/
@RestController
@RequestMapping("api/work/place")
public class WorkPlaceController {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private WorkPlaceService workPlaceService;
    /**
     * 查询所有
     */
    @RequestMapping("/query-all")
    public ReqResult queryAll() {
        logger.info("地点表 查询所有数据");
        List<WorkPlaceEntity> list = workPlaceService.queryAll();
        return ReqResult.ok().put("list", list);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public ReqResult save(@RequestBody WorkPlaceEntity workPlaceEntity) {
        logger.info("地点表 保存信息");
        if (workPlaceEntity.getTitle().equals("添加地点信息")) {
            workPlaceEntity.setCreateTime(new Date());
            workPlaceService.save(workPlaceEntity);
        } else {
            workPlaceService.updateById(workPlaceEntity);
        }
        return ReqResult.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public ReqResult delete(@RequestBody Map<String, Object> params) {
        logger.info("地点表 根据id删除");
        int id = (Integer) params.get("id");
        workPlaceService.removeById(id);
        return ReqResult.ok();
    }
}